#Region " Imports "

Imports BlazeApps.Library.Subscriptions

#End Region

Partial Class Usercontrols_Options_Favorites
    Inherits UserControlBase

#Region " Enums, Memebers and Properties "

    Public Property ItemGuid() As Guid
        Get
            Return GetGuidFromVS("ItemGuid")
        End Get
        Set(ByVal value As Guid)
            ViewState.Add("ItemGuid", value)
        End Set
    End Property

    Public Property FavoriteType() As Integer
        Get
            Return GetIntegerFromVS("FavoriteType")
        End Get
        Set(ByVal value As Integer)
            ViewState.Add("FavoriteType", value)
        End Set
    End Property

    Public Property HideData() As Boolean
        Get
            Return GetBooleanFromVS("HideData")
        End Get
        Set(ByVal value As Boolean)
            ViewState.Add("HideData", value)
        End Set
    End Property

#End Region

#Region " Page Events "

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

        If ItemGuid = Guid.Empty Then
            GetQueryStringValues()
        End If

        SetPageState()

        If Me.Page.IsPostBack = False Then
            CheckFavorites()
        End If

    End Sub

#End Region

#Region " Save Data "

    Private Sub SaveFavorite()
        Dim Favorite As Subscription = Subscription.NewSubscription

        Favorite.itemid = ItemGuid
        Favorite.Userid = CurrentUserID
        Favorite.ivSubscriptionType = FavoriteType
        Favorite.Save()

        Me.uxAddToFavoritesLinkButton.Text = Resources.Resource.AddedToFavorites.ToString()
        Me.uxAddToFavoritesLinkButton.Enabled = False

    End Sub

#End Region

#Region " Load Data "

    Public Sub CheckFavorites()

        If Not HideData Then
            If IsUserAuthenticated = True Then
                If Not (ItemGuid = Guid.Empty) Then
                    Dim Favorite As Subscription = Subscription.GetSubscription(ItemGuid, CurrentUserID)

                    If Favorite.HasData = True Then
                        Me.uxAddToFavoritesLinkButton.Text = Resources.Resource.AddedToFavorites.ToString()
                        Me.uxAddToFavoritesLinkButton.Enabled = False
                    Else
                        Me.uxAddToFavoritesLinkButton.Text = Resources.Resource.AddToFavorites.ToString()
                    End If
                End If
            End If
        End If

    End Sub

#End Region

#Region " Form Controls Events "

    Protected Sub uxAddToFavoritesLinkButton_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles uxAddToFavoritesLinkButton.Click
        SaveFavorite()
    End Sub

#End Region

#Region " Helpers and Methods "

    Protected Sub GetQueryStringValues()
        ItemGuid = GetGuidFromQueryString("id")
    End Sub

    Private Sub SetPageState()

        Me.uxLoggedInPanel.Visible = IsUserAuthenticated()
        Me.uxPleaseLoginPanel.Visible = Not IsUserAuthenticated()

    End Sub

#End Region

End Class
